from django.contrib import admin
from .models import Category, Post, Tag
from .models import Comment

admin.site.register(Tag) 

# 注册 Category 模型的自定义 admin 类
class CategoryAdmin(admin.ModelAdmin):
    list_display = ('name', 'desc', 'weight', 'icon')
    list_filter = ('desc',)  # 假设您可能需要按描述过滤
    search_fields = ('name', 'desc')
    ordering = ['weight']  # 根据权重排序

admin.site.register(Category, CategoryAdmin)


class PostAdmin(admin.ModelAdmin):
    ''' 文章详情管理 '''

    list_display = ('id', 'title', 'category', 'display_tags', 'owner', 'pv', 'is_hot', 'pub_date')
    list_filter = ('owner',)
    search_fields = ('title', 'desc')
    list_editable = ('is_hot',)
    list_display_links = ('id', 'title',)

    def display_tags(self, obj):
        """自定义方法来显示标签"""
        return ", ".join([tag.name for tag in obj.tags.all()]) if obj.tags.exists() else "无标签"
    display_tags.short_description = '标签'

    # Media 定义可以保持不变
    class Media:
        css = {
            'all': ('ckeditor5/cked.css',)
        }
        js = (
            'https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js', 
            'ckeditor5/ckeditor.js',
            'ckeditor5/translations/zh.js',
            'ckeditor5/config.js'
        )

# 注册 Post 模型和自定义的 PostAdmin
admin.site.register(Post, PostAdmin)




class CommentAdmin(admin.ModelAdmin):
    list_display = ['user', 'post', 'content', 'created_at']
    search_fields = ['user__username', 'post__title', 'content']
    list_filter = ['created_at']

admin.site.register(Comment, CommentAdmin)